package org.example.panel.scorepanel;
import javax.swing.*;
import java.awt.event.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class DeleteScorePanel extends JPanel {
    public DeleteScorePanel(){
        //组件准备
        JTextField num = new JTextField(15);
        JTextField courseName = new JTextField(15);
        JButton addScore = new JButton("删除成绩");
        Box numBox = Box.createHorizontalBox();
        Box courseNameBox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        addScore.addActionListener((ActionEvent e)->{
            if(num.getText().isEmpty()||courseName.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null,
                        "信息不能为空", null, JOptionPane.WARNING_MESSAGE);
            }else if(num.getText().matches(".*\\s+.*") ||
                    courseName.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "信息不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(StudentOperation.isNotStudent(num.getText())){
                JOptionPane.showMessageDialog(null,
                        "学生不存在", null, JOptionPane.WARNING_MESSAGE);
            }else if(!CourseOperation.isCourse(courseName.getText())) {
                JOptionPane.showMessageDialog(null,
                        "课程管理中无此课程", null, JOptionPane.WARNING_MESSAGE);
            }else if(!ScoreOperation.isScore(num.getText(), courseName.getText())) {
                JOptionPane.showMessageDialog(null,
                        "该学生不存在该课程成绩", null, JOptionPane.WARNING_MESSAGE);
            }else{
                ScoreOperation.deleteScore(num.getText(), courseName.getText());
                JOptionPane.showMessageDialog(null,
                        "删除成绩成功", null, JOptionPane.WARNING_MESSAGE);
            }
        });
        //添加组件
        numBox.add(new JLabel("学号： "));
        numBox.add(num);
        courseNameBox.add(new JLabel("课程名： "));
        courseNameBox.add(courseName);
        box.add(numBox);
        box.add(Box.createVerticalStrut(5));
        box.add(courseNameBox);
        box.add(Box.createVerticalStrut(5));
        box.add(addScore);
        add(box);
    }
}
